<?php

/**
 * This is the model class for table "Autor".
 *
 * The followings are the available columns in table 'Autor':
 * @property integer $id
 * @property string $nome
 * @property string $email
 * @property integer $idUsuario
 * @property string $bio
 *
 * The followings are the available model relations:
 * @property Usuario $idUsuario0
 * @property Oa[] $oas
 */
class Autor extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Autor the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'Autor';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('nome', 'required'),
//			array('idUsuario', 'numerical', 'integerOnly'=>true),
			array('nome, email', 'length', 'max'=>80),
			array('bio', 'length', 'max'=>300),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, nome, email, bio', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'idUsuario0' => array(self::BELONGS_TO, 'Usuario', 'idUsuario'),
			'oas' => array(self::HAS_MANY, 'Oa', 'idAutor'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'nome' => 'Nome',
			'email' => 'Email',
			'idUsuario' => 'Id Usuario',
			'bio' => 'Bio',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('nome',$this->nome,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('idUsuario',$this->idUsuario);
		$criteria->compare('bio',$this->bio,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}

	protected function beforeSave(){
		$usuario = Usuario::model()->find('email = :email', array(':email' => Yii::app()->user->name));
		if($usuario != null)
			$this->idUsuario = $usuario->id;
		return true;	
	}
}
